/*
 * fbank.com Inc.
 * Copyright (c) 2020 All Rights Reserved.
 * create by fm
 * date:2020-04-26
 */
package com.ouwen.smartpay.service.impl;

import com.fm.core.common.service.EntityServiceImpl;
import com.ouwen.smartpay.dao.PartnerServiceDao;
import com.ouwen.smartpay.entity.PartnerService;
import com.ouwen.smartpay.service.PartnerServiceService;
import com.ouwen.smartpay.service.cache.CacheKeys;
import com.ouwen.smartpay.service.cache.LocalCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.ouwen.smartpay.service.cache.CacheKeys.buildKey;

/**
 * 商户服务管理 Service实现
 * <p>
 * Date: 2020-04-26 10:59:48
 *
 * @author fm
 */
@Service("partnerServiceService")
public class PartnerServiceServiceImpl extends EntityServiceImpl<PartnerService, PartnerServiceDao> implements PartnerServiceService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private LocalCacheService localCacheService;

    @Override
    public List<PartnerService> findPartnerServices(String parnerId, String subPartnerId) {
        try {

            return localCacheService.loadCache(buildKey(CacheKeys.OPENAPI_PARTNER_SERVICE_CACHEKEY, parnerId, subPartnerId), () ->
                    getEntityDao().findPartnerServices(parnerId, subPartnerId)
            );

        } catch (Exception e) {

            logger.error("商户服务管理加载缓存异常:", e);

            return getEntityDao().findPartnerServices(parnerId, subPartnerId);

        }
    }

    @Override
    public void updatePartnerService(PartnerService partnerService) {

        localCacheService.evitCache(buildKey(CacheKeys.OPENAPI_PARTNER_SERVICE_CACHEKEY, partnerService.getPartnerId(), partnerService.getSubPartnerId()));

        getEntityDao().update(partnerService);

    }

}
